<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,app.stock.common.*,app.stock.web.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" defer>

    function isStockTransaction(txType) {
        if (hasValue(txType)) {
            return (txType == '<%=TransactionTypes.mairu%>' || txType == '<%=TransactionTypes.maichu%>');
        }
        return false;
    }
    
    function setStockName(txCode){
    	var code = document.getElementsByName('<%=Transaction.codeName%>')[0];
    	var name = document.getElementsByName('<%=Transaction.nameName%>')[0];
    	if (window.XMLHttpRequest){
        	xmlhttp=new XMLHttpRequest();
    	}
    	xmlhttp.onreadystatechange = function(){
        	if (xmlhttp.readyState==4 && xmlhttp.status==200){
        	    name.value=xmlhttp.responseText;
        	}
        }
    	xmlhttp.open("GET","StockCtrl?action=ajax&command=getStockList&code="+code.value,true);
    	xmlhttp.send();
    }

    function calcSum(){
        var price = document.getElementsByName('<%=Transaction.priceName%>')[0].value;
        var amount = document.getElementsByName('<%=Transaction.amountName%>')[0].value;
        var txType = document.getElementsByName('<%=Transaction.typeName%>')[0].value;
        if(txType == '<%=TransactionTypes.mairu%>' && amount<0){
        	amount=-amount;
        }
        else if(txType == '<%=TransactionTypes.maichu%>' && amount>0){
            amount=-amount;
        }
        document.getElementsByName('<%=Transaction.amountName%>')[0].value=amount;
        var sum1 = - ( price * amount );
        sum1 = sum1.toFixed(2);
        document.getElementsByName('<%=Transaction.sum1Name%>')[0].value=sum1;
        var shouxufei = document.getElementsByName('<%=Transaction.shouxufeiName%>')[0].value;
        if(!shouxufei){
        	shouxufei=0;
        }
        if(shouxufei>0){
        	shouxufei=-shouxufei;
        }
        if(shouxufei<0){
            document.getElementsByName('<%=Transaction.shouxufeiName%>')[0].value=parseFloat(shouxufei).toFixed(2);
        }
        var yinhuashui = document.getElementsByName('<%=Transaction.yinhuashuiName%>')[0].value;
        if(!yinhuashui){
        	yinhuashui=0;
        }
        if(yinhuashui>0){
        	yinhuashui=-yinhuashui;
        }
        if(yinhuashui<0){
            document.getElementsByName('<%=Transaction.yinhuashuiName%>')[0].value=parseFloat(yinhuashui).toFixed(2);
        }
        var guohufei = document.getElementsByName('<%=Transaction.guohufeiName%>')[0].value;
        if(!guohufei){
        	guohufei=0;
        }
        if(guohufei>0){
        	guohufei=-guohufei;
        }
        if(guohufei<0){
            document.getElementsByName('<%=Transaction.guohufeiName%>')[0].value=parseFloat(guohufei).toFixed(2);
        }
        var sum2 = parseFloat(sum1) + parseFloat(shouxufei) + parseFloat(yinhuashui) + parseFloat(guohufei);
        sum2 = parseFloat(sum2).toFixed(2);
        if(sum2){                       
            document.getElementsByName('<%=Transaction.sum2Name%>')[0].value=sum2;
        }
    }
    
    function  checkForm(){
        var type = document.getElementsByName('<%=Transaction.typeName%>')[0];
        if(!hasValue(type.value)){
            //type = new Option('<%=TransactionTypes.maichu%>');
            return false;
        }
        if(isStockTransaction(type.value)){
            var code = document.getElementsByName('<%=Transaction.codeName%>')[0];
            if(!hasValue(code.value)){
            	code.value='Value is required';
                return false;
            }
            var sum1 = document.getElementsByName('<%=Transaction.sum1Name%>')[0];
            if(!hasValue(sum1.value)){
                sum1.value='Value is required';
                return false;
            }   
        }
        return true;        
    }

    function toFixedFloatByName(elemNames,tagName){
        var two = 2;
    	if(!elemNames){
           return;
    	}
    	for(i=0; i< elemNames.length;i++){
           var elemName = elemNames[i];    	   
    	   var temp= $(tagName+'[name='+elemName+']');
    	   for(j = 0; j < temp.length; j++){
    	    temp[j].innerHTML = parseFloat(temp[j].innerHTML).toFixed(two);
    	    }
    	}
    }
    
    function toFixedPercentageByName(elemNames,tagName){
        var two = 2;
        if(!elemNames){
            return;
        }
        for(i=0; i< elemNames.length;i++){
            var elemName = elemNames[i];        
            var temp= $(tagName+'[name='+elemName+']');
            for(j = 0; j < temp.length; j++){
            	temp[j].innerHTML = (parseFloat(temp[j].innerHTML)*100).toFixed(two)+'%';
             }
        }
 }
</script>
</head>
</html>